#include "roomListCommand.h"

QDomDocument RoomListCommand::run(QDomDocument doc) {
    QDomDocument xmlDocument;
    QString result;

    if (this->isAuthorized(doc)) {
        QList<Room*> rooms = DB::getInstance()->getRooms();
        result = "<roomsList><success>TRUE</success>";
        for (int i = 0; i < rooms.size(); i++) {
            result.append("<room><name>")
                .append(rooms.at(i)->getName())
                .append("</name><needPass>")
                .append(rooms.at(i)->getPassNeed() ? "TRUE" : "FALSE")
                .append("</needPass></room>");
        }
        result.append("</roomList>");
    } else {
        result = "<roomsList><success>FALSE</success><reason>NOT_AUTHORIZED</reason></roomsList>";
    }

    xmlDocument.setContent(result);
    return xmlDocument;
}
